跳到主要内容

Go 测试框架 goConvey 学习

测试框架 goConvey 项目地址

goConvey 是什么?

GoConvey 是一款针对 Golang 的测试框架

$ go get github.com/smartystreets/goconvey

快速使用

创建几个简单的方法

package main

func SayHello(name string) string {
return "Hello" + name + "!"
}

func Add(a, b int) int {
return a + b
}

编写测试

package main

import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)

func TestSayHello(t *testing.T) {
Convey("TestSayHello should say hello~", t, func() {
a := "Lisa"
b := "Alan"
So(SayHello(a), ShouldEqual, "Hello"+a+"!")
So(SayHello(b), ShouldEqual, "Hello"+b+"!")
})
}

func TestAdd(t *testing.T) {
Convey("将两数相加", t, func() {
So(Add(1, 2), ShouldEqual, 3)
})
}

返回值

=== RUN   TestSayHello
..
2 total assertions

--- PASS: TestSayHello (0.00s)
=== RUN TestAdd
.
3 total assertions

--- PASS: TestAdd (0.00s)
PASS

注意,这里除了 ShouldEqual 还提供很多其它的断言

Web 页面

在相应目录下执行 goconvey(上面已经通过 go get 自动安装到 $GOPATH/bin 目录下了),然后打开浏览器,访问 http://localhost:8080

goconvey